<template>
  <div class="play-list">
    <sort-bar>
      <template #left>全部</template>
      <template #center>
        <div class="center" @click.stop="classClick">
          <span ref="item">选择分类</span>
          <i></i>
        </div>
      </template>
      <template #right>
        <div class="right">
          <button
            @click="buttonChange('hot')"
            :class="{btnactive: buttonActive}"
            :disabled="buttonActive"
          >热门</button>
          <button
            @click="buttonChange('new')"
            :class="{btnactive: !buttonActive}"
            :disabled="!buttonActive"
          >最新</button>
        </div>
      </template>
    </sort-bar>
    <toast
      :isshow="isshow"
      :cate="categories"
      :category="category"
      :catedetail="catedetail"
      @cateClick="cateClick"
    ></toast>
    <playlist-item :playlists="playlists"></playlist-item>
    <el-pagination
      v-if="playlists"
      background
      layout="prev, pager, next"
      :page-size="35"
      :total="total"
      :current-page.sync="page"
      @current-change="currentChange"
      @prev-click="currentChange"
      @next-click="currentChange"
    ></el-pagination>
  </div>
</template>

<script>
import SortBar from '@/components/content/sortbar/SortBar'

import Toast from './childcpns/Toast'
import PlaylistItem from './childcpns/PlaylistItem'

import { _getAllPlaylist, _getCat } from '@/network/playlist'

export default {
  components: {
    SortBar,
    Toast,
    PlaylistItem
  },
  data() {
    return {
      isshow: true,          //控制toast是否隐藏
      playlists: null, 
      total: 0,              //控制页码数量
      categories: {},
      category: [],
      catedetail: {},
      cat: '全部',            //控制歌单种类
      buttonActive: true,
      order: 'hot',           //控制热门/最新
      page: 1
    }
  },
  created() {
    // 获取初始歌单
    _getAllPlaylist(0, this.cat, this.order).then(res => {
      this.playlists = res.playlists
      this.total = res.total
    })
    // 获取歌单的种类
    _getCat().then(res => {
      this.categories = res
      for (let key in res.categories) {
        this.category.push(res.categories[key])
      }
      // 歌单种类再分类
      for (let i of res.sub) {
        if (this.catedetail[i.category]) {
          this.catedetail[i.category].push(i)
        } else {
          this.catedetail[i.category] = [i]
        }
      }
    })
  },
  mounted() {
    // 鼠标点击其它区域toast隐藏
    document.body.addEventListener('click', () => {
      this.isshow = true
    })
  },  
  methods: {
    classClick() {
      this.isshow = !this.isshow
    },
    // 页码改变修改歌单
    currentChange(page) {
      _getAllPlaylist((page - 1) * 35, this.cat, this.order).then(res => {
        this.playlists = res.playlists
        this.total = res.total
      })
      document.documentElement.scrollTop = 0
    },
    // 切换类型修改歌单
    cateClick(category) {
      this.cat = category
      this.isshow = true                        //选完隐藏toast
      this.$refs.item.innerText = category      //修改分类标签里文字
      this.page = 1                             //页码回归1
      _getAllPlaylist(0, this.cat, this.order).then(res => {
        this.playlists = res.playlists
        this.total = res.total
      })
    },
    // 切换最新/热门修改歌单
    buttonChange(order) {
      this.buttonActive = !this.buttonActive
      this.order = order
      this.page = 1
      _getAllPlaylist(0, this.cat, this.order).then(res => {
        this.playlists = res.playlists
        this.total = res.total
      })
    }
  }
}
</script>

<style scoped>
.play-list {
  position: relative;
  z-index: 1;
  width: 980px;
  margin: 0 auto;
  padding: 40px;
  border-left: 1px solid #666;
  border-right: 1px solid #666;
}

/* sortbar区域 */
.sort-bar >>> .left {
  flex: 3;
}
.center {
  position: relative;
  padding-right: 10px;
  margin-top: 5px;
  width: 90px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  background: url(~@/assets/img/button.png) 0 -305px;
  color: #0c73c2;
  border: 1px solid #666;
  border-radius: 5px;
  cursor: pointer;
}
.center:hover {
  background-position-y: -141px;
}

/* 下拉箭头 */
.center i {
  position: absolute;
  top: 12px;
  right: 8px;
  width: 10px;
  height: 10px;
  box-sizing: border-box;
  border-width: 5px;
  border-style: solid;
  border-color: #666 transparent transparent transparent;
}

/* 切换最新/热门 */
button {
  width: 40px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  font-weight: 600;
  border: none;
  outline-style: none;
  border-radius: 5px 0 0 5px;
  color: #ccc;
  background: url(~@/assets/img/button.png) -5px -2855px;
  cursor: pointer;
}
.right button:nth-child(2) {
  border-radius: 0 5px 5px 0;
}

.btnactive {
  background-position-y: -141px;
  color: #000;
  cursor: auto;
}

/* 修改页码默认样式 */
.el-pagination {
  padding: 0 240px;
  margin: 30px auto 0 !important;
}
.el-pagination >>> .number,
.el-pagination >>> .btn-prev,
.el-pagination >>> .btn-next,
.el-pagination >>> .more {
  border: 1px solid #444 !important;
  background: #444 !important;
  color: #ccc !important;
}
.el-pagination >>> .number:hover,
.el-pagination >>> .btn-prev:hover,
.el-pagination >>> .btn-next:hover,
.el-pagination >>> .more:hover {
  border-color: #fff !important;
}
.el-pagination >>> .active {
  background-color: #fff !important;
  border-color: #fff !important;
  color: #000 !important;
}
.el-pagination >>> .active:hover {
  color: #000 !important;
}
</style>